package com.ckf.login_wx.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckf.login_wx.entity.Layui;
import com.ckf.login_wx.entity.Material;
import com.ckf.login_wx.entity.Odd;
import com.ckf.login_wx.entity.Repertory;
import com.ckf.login_wx.entity.Xinsell;
import com.ckf.login_wx.servic.MaterialService;
import com.ckf.login_wx.util.Getpo;
import com.ckf.login_wx.util.Idname;
import com.ckf.login_wx.util.Jy;
import com.github.pagehelper.PageHelper;

@RestController
@RequestMapping("/Sp")
public class SpMaterialcontroller {
	@Autowired
    private MaterialService materialService;
	Jy jy=new Jy();
	
    @GetMapping("/ordlistallbysaisie")
    public Object ordlistallbysaisie(String saisie){
    	QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put(Idname.getname("saisie"),saisie);
        queryWrapper.allEq(mapuser);
        return materialService.list(queryWrapper);
    }
    @GetMapping("/ordmaterialbysearch")
    public Object ordmaterialbysearch(String search,String talk,String pagenew){//
        Integer i = Integer.parseInt(pagenew);
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put(Idname.getname(search),talk);
        queryWrapper.allEq(mapuser);
   	 	PageHelper.offsetPage((i-1)*5, 5);
        return materialService.list(queryWrapper);
    }
    @GetMapping("/quxiansearch")
    public Object quxiansearch(String search,String talk,String pagenew,String saisie){//
        Integer i = Integer.parseInt(pagenew);
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put(Idname.getname(search),talk);
        mapuser.put("saisie",saisie);
        queryWrapper.allEq(mapuser);
   	 	PageHelper.offsetPage((i-1)*5, 5);
        return materialService.list(queryWrapper);
    }
    @RequestMapping("/selematerialbyid")
    public Object selematerialbyid(int id) throws Exception{
    	//Integer idint = Integer.parseInt(id);
        return materialService.getById(id);
    }
    @RequestMapping("/sele")
    public Object sele(HttpServletRequest request){
    	
    	String pageNo=request.getParameter("page");
		String pagesize=request.getParameter("limit");
    	Integer i = Integer.parseInt(pageNo); 
    	Integer n = Integer.parseInt(pagesize); 
    	Map<String, Object> map = new HashMap<>();
    	List<Material> a= materialService.list();
    	if(n!=5000)PageHelper.offsetPage((i-1)*n, n);
        map.put("data",materialService.list());
    	//制单员和销售只能看自己的
        HttpSession session= request.getSession();
        String username = session.getAttribute("username")+"";
        String quxian = session.getAttribute("quxian")+"";
        String truename = session.getAttribute("truename")+"";
    	if(quxian.equals("2")||quxian.equals("11")){
    		System.out.println("进入角色："+truename);

        	a= materialService.list((QueryWrapper<Material>)Getpo.one("saisie", truename));
        	if(n!=5000)PageHelper.offsetPage((i-1)*n, n);
            map.put("data",materialService.list((QueryWrapper<Material>)Getpo.one("saisie", truename)));
    	}
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
    return map;
    }
    @RequestMapping("/selematesearch")
    public Object selematesearch(HttpServletRequest request,String modele,String name,String type,String time,String saisie){
    	System.out.println(time+"*************"+name+saisie);
    	String pageNo=request.getParameter("page");
		String pagesize=request.getParameter("limit");
    	Integer i = Integer.parseInt(pageNo); 
    	Integer n = Integer.parseInt(pagesize); 
    	
    	QueryWrapper<Material> queryWrapper = new QueryWrapper<>();

        queryWrapper.like("modele", modele)
		.like("name", name)
		.like("type", type);
        if(time!=null&&!time.equals("")&&time.length()>13&&time.indexOf("-")!=-1){
            String timestart=time.substring(0,10);
            String timeend=time.substring(12);
            queryWrapper.le("time", timeend);
            queryWrapper.ge("time", timestart);
            
        }
    	
      //制单员和销售只能看自己的，则多添加一个mapuser.put
        HttpSession session= request.getSession();
        String username = session.getAttribute("username")+"";
        String quxian = session.getAttribute("quxian")+"";
        String truename = session.getAttribute("truename")+"";
        
        Map<String, Object> mapuser = new HashMap<>();
    	if(quxian.equals("2")||quxian.equals("11")){
    		System.out.println("进入角色："+truename);

    		mapuser.put("saisie",truename);
    	}
        queryWrapper.allEq(mapuser);
        
        
        
    	Map<String, Object> map = new HashMap<>();
    	List<Material> a= materialService.list(queryWrapper);
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
    	if(n!=5000)PageHelper.offsetPage((i-1)*n, n);
        map.put("data",materialService.list(queryWrapper));
        return map;
    }
    @RequestMapping("/seles")
    public Object selebyid(String inputvalue){
    	Map<String, Object> map = new HashMap<>();
    	
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put("name",inputvalue);
        queryWrapper.allEq(mapuser);
    	List<Material> a= materialService.list(queryWrapper);
    	if(inputvalue.equals("")||inputvalue==null)
    		a= materialService.list();
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
        map.put("data",a);
    return map;
    }
	@PostMapping("/addmate")
	public Object addmate(int id,String name, String type,String time, String unite,String saisie,String modele, double money,int quantite,String faf) throws ParseException{
	    String stu="";
	    Material material=new Material();
	    material.setName(name);
	    material.setModele(modele);
	    material.setQuantite(quantite);
	    material.setType(type);
	    material.setMoney(money);
	    material.setTotal(quantite*money);
	    material.setUnite(unite);
	    material.setSaisie(saisie);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        material.setTime(simpleDateFormat.parse(time));
		if(faf.equals("add")){
	        if (materialService.save(material)){
	        	stu="添加成功";
	        }else {
	        	stu="添加失败";
	        }
		}
		if(faf.equals("up")){
			material.setId(id);
	        if (materialService.updateById(material)){
	        	stu="修改成功";
	        }else {
	        	stu="修改失败";
	        }
		}
	    return stu;
	}
    @GetMapping("/delemate")
    public Object delemate(Integer id){
    	Map<String, Object> map = new HashMap<>();
        if (materialService.removeById(id)){
            map.put("result","删除成功");
        }else {
            map.put("result","删除失败");
        }
    return map;
    }

    @RequestMapping("/delemateall")
    public Object delemateall(String[] id){
    	
    	for (int i = 0; i < id.length; i++) {
    		try {
    			if(id[i].equals(""))continue;
    	    	int idint = Integer.parseInt(id[i]+"");
    	    	materialService.removeById(idint);
			} catch (Exception e) {
				System.out.println("***********************************异常*************************************");
				return "异常";
			}
		}
		return "删除成功";
    }
    @RequestMapping("/matetotalInfo")
    public Object matetotalInfo(String modele,String name,String type,String time){
    	System.out.println(modele+"******************"+name);
    	QueryWrapper<Material> queryWrapper = new QueryWrapper<>();

        queryWrapper.like("modele", modele)
		.like("name", name)
		.like("type", type);
        if(time!=null&&!time.equals("")&&time.length()>13&&time.indexOf("-")!=-1){
            String timestart=time.substring(0,10);
            String timeend=time.substring(12);
            queryWrapper.le("time", timeend);
            queryWrapper.ge("time", timestart);
        }
    	double total=0;int quantite=0;
    	List<Material> mate=materialService.list(queryWrapper);
    	for (int i = 0; i < mate.size(); i++) {
			total+=mate.get(i).getMoney();
			quantite+=mate.get(i).getQuantite();
			
		}
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("quantite",quantite);  
        return map;
    }
    @RequestMapping("/matetotal")
    public Object matetotal(){
    	double total=0;int quantite=0;
    	List<Material> mate=materialService.list();
    	for (int i = 0; i < mate.size(); i++) {
			total+=mate.get(i).getMoney();
			quantite+=mate.get(i).getQuantite();
		}
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("quantite",quantite);  
        return map;
    }
    //查询列表内型号
	@RequestMapping("/selematebymodele")
    public Object selematebymodele(String odd){
		List<String> modele =new ArrayList<String>();
    	List<Material> mate=materialService.list();
		for (int i = 0; i < mate.size(); i++) {
			if(!modele.contains(mate.get(i).getModele())){
				modele.add(mate.get(i).getModele());
			}
		}
        Map<String, Object> map = new HashMap<>();
        map.put("modele",modele);
		return map;
	}
}
